<?php
require_once "ugovori.class.php";
class ugo_report extends ugovori{
	var $db;
	var $_filterLen;
	function ugo_report(){
		require_once "db_functions.php";
		$this->db = new db_functions();
		$this->loadConfig();
	}
	function _get($sql){
		$res = parent::_get($sql);
		return $res;
	}
	
	function showReport($t,$params,$page){
		switch($t){
			case 1:
				$this->reportIzd($params);
				break;
			case 2:
				$this->reportUlaz($params);
				break;
			case 21:
				$this->reportUlazVracanje();
				break;
			case 3:
				$radnik_id = $_GET["rid"];
				if($radnik_id){
					$this->reportRadnik(array("id"=>$radnik_id,"first"=>$_GET["rname"],"last"=>$_GET["rprez"]));
				}else{
					$this->reportRadnikSearch($_GET["radnik"]);
				}
				break;
			case 4:
				$params["id"] = $_GET["id"];
				$params["tip"] = $_GET["tip"];
				$params["od"] = trim($_GET["od"]);
				$params["do"] = trim($_GET["do"]);
				$params["first"] = $_GET["ime"];
				$params["last"] = $_GET["prez"];
				$this->reportRadnik($params);
				break;
			case 5:
				$this->reportMat($params,$page);
				break;
			case 6:
				$this->reportUlazIzlaz();
				break;
			case 7:
				$this->reportRazduzenje();
				break;
		}
	}
	
	
	function reportRazduzenje(){
		$day = 60*60*24;
		
		// ------------------- FILTER
		
		$filter_brugo = getFromInput("brugo");
		$filter_datum = getFromInput("datum");
		$filter_datum1 = getFromInput("datum1");
		$filter_radnik = getFromInput("radnik");
		
		if(!preg_match('/\s*\d{1,2}\.\d{1,2}\.\d{2,4}\s*/',$filter_datum1)){
			$filter_datum1 = "";
		}
		
		$bool_dat	= (strlen(trim($filter_datum))>0) && (preg_match('/\s*\d{1,2}\.\d{1,2}\.\d{2,4}\s*/',$filter_datum))	?true	:false;
		$bool_dat1	= strlen(trim($filter_datum1))>0	?true	:false;
		$bool_radnik= $filter_radnik>0				?true	:false;
		$bool_brugo	= strlen(trim($filter_brugo))>0	?true	:false;
		
		//var_dump($bool_dat);var_dump($bool_dat1);var_dump($bool_radnik);var_dump($bool_brugo);
		
		if(!$bool_dat && !$bool_dat1 && !$bool_radnik && !$bool_brugo){
			$filter_datum = date("d.m.Y");
		}
		
		$this->assign("filter_brugo",$filter_brugo);
		$this->assign("filter_datum",$filter_datum);
		$this->assign("filter_datum1",$filter_datum1);
		
		$polja = $this->get_Radnici($filter_radnik);
			$this->assign("radnik_id",$polja["polje_id"]);
			$this->assign("radnik_ime",$polja["polje_ime"]);
			$this->assign("radnik_prezime",$polja["polje_prezime"]);
			$this->assign("radnik_selected",$polja["polje_selected"]);
			
			
		// --------------------- MAIN
		$q = "SELECT ur.*, ui.br_ugovora, ui.datum as datum1, ui.radnik as radnik1 "
			."FROM ugovori_izdavanje ui, ugovori_razduzivanje ur "
			."WHERE ui.id=ur.izdavanje_id";
			
		if($filter_datum){
			$tmp = explode(".",$filter_datum,3);
			$filter_datum_start = makeUnixTime(0,0,0,$tmp[1],$tmp[0],$tmp[2]);
			//$filter_datum_end = $filter_datum_start+$day;
			
			//$q .= "AND ur.datum>=$filter_datum_start AND ur.datum<$filter_datum_end";
			$q .= " AND ur.datum>=$filter_datum_start";
		}
		if($filter_datum1){
			$tmp = explode(".",$filter_datum1,3);
			$filter_datum_end = makeUnixTime(23,59,59,$tmp[1],$tmp[0],$tmp[2]);
			$q .= " AND ur.datum<=$filter_datum_end";
		}
		if($filter_radnik>0){
			$q .= " AND ur.radnik=$filter_radnik";
		}
		if($filter_brugo){
			$q .= " AND ui.br_ugovora like '%$filter_brugo%'";
		}
		
		$q .= " ORDER BY datum desc";
		$res = $this->db->select2($q);
		
		if($res !== false){
		
			foreach ($res as $k=>$row){
				$rowid[] = $k;
				$dats[]		= date("d.m.Y H:i:s",$row["datum"]);
				$dats1[]	= date("d.m.Y H:i:s",$row["datum1"]);
				
				$q = "SELECT radnik_ime, radnik_prezime FROM radnici WHERE radnik_id=".$row["radnik"];
				list($ime, $prezime) = $this->db->getRow($q);
				$radnik[]	= "$ime $prezime";
				
				
				$q = "SELECT radnik_ime, radnik_prezime FROM radnici WHERE radnik_id=".$row["radnik1"];
				list($ime, $prezime) = $this->db->getRow($q);
				$radnik1[]	= "$ime $prezime";
				
				$brugos[]	= $row["br_ugovora"];
			}
			
			$this->assign("row_id",$rowid);
			$this->assign("datum",$dats);
			$this->assign("datum1",$dats1);
			$this->assign("radnik",$radnik);
			$this->assign("radnik1",$radnik1);
			$this->assign("brugo",$brugos);
			
			$this->assign("info","Ukupno: ".count($res));
		}else{
			$this->assign("comment_start","<!--");
			$this->assign("comment_end","-->No results");
		}
		
		echo $this->parseTemplate("ugovori/report_razduzenje.tpl");
	}
	
	function reportIzd($params){
		$pr_sum=0;
		$max_ugo=0;
		$start = strlen($params["start"])>0?$params["start"]:date("d.m.Y");
		$end = $params["end"];
		$bu = $params["br_ug"];
		$rad = $params["rad"];
		$q = "SELECT * FROM ugovori_izdavanje";
		$filter_status = "";
		if($start || $end || $bu){$q .= " WHERE ";}
		if($start){
			$this->assign("start",$start);
			$filter_status .= "Datum od=".$start.", ";
			$tmp = explode(".",$start);
			$start = makeUnixTime(0,0,0,$tmp[1],$tmp[0],$tmp[2]);
		}
		if($end){
			$this->assign("end",$end);
			$filter_status .= "Datum do=".$end.", ";
			$tmp = explode(".",$end);
			$end = makeUnixTime(0,0,0,$tmp[1],$tmp[0],$tmp[2]);
		}
		$end = $end?$end+(60*60*24):$end;
		$prev = false;
		//DATUM VECI OD
		if($start){
			$q .= "datum >= ".$start;
			$prev = true;
		}
		
		// DATUM MANJI OD
		if($end){
			if($prev)
				$q .= " AND";
			$q .= " datum <= ".$end;
			$prev = true;
		}
		//PO BROJU UGOVORA
		if($bu){
			$this->assign("filter_br_ugo",$bu);
			$filter_status .= "Broj ugovora=".$bu.", ";
			if($prev)
				$q .= " AND";
			$q .= " br_ugovora LIKE '%".$bu."%'";
			$prev = true;
		}
		// PO RADNIKU
		if(strlen(trim($rad))){
			$this->assign("filter_radnik",$rad);
			$r = $this->_get("SELECT radnik_id FROM radnici WHERE CONCAT(radnik_ime,' ',radnik_prezime) LIKE '%".$rad."%'");
			if($r){
				if(!$prev)
					$q .= " WHERE";
				else
					$q .= " AND";
				$q.=" (";
				foreach($r as $k=>$v){
					$q .= " radnik=".$v["radnik_id"];
					if($k < (count($r)-1))
						$q .= " OR ";
				}
				$q .= ")";
			}else{
				if($prev)
					$q .= " AND radnik=-1";
				else
					$q .= " WHERE radnik=-1";
			}
		}
		
		$q.=" ORDER BY datum DESC, br_ugovora DESC";
		$res = $this->_get($q);
		$this->_filterLen = count($res);
		$dates = $br_ugo = $kol_pr = $radnik = $kom = array();
		
		if($res){
			foreach($res as $v){
				$max_ugo = ($v["br_ugovora"] > $max_ugo) ? $v["br_ugovora"] : $max_ugo;
				$pr_sum += $v["kol_predracuna"];
				$dates[] = date($this->getFromConfig("short_date"),$v["datum"]);
				$br_ugo[] = $v["br_ugovora"];
				$kol_pr[] = $v["kol_predracuna"]." ";
				$rad = $this->_get("SELECT radnik_ime, radnik_prezime FROM radnici WHERE radnik_id=".$v["radnik"]." LIMIT 1");
				$rad = $rad[0];
				$radnik[] = $rad["radnik_ime"]." ".$rad["radnik_prezime"];
				$kom[] = nl2br($v["komentar"]);
				$storno[] = ($v["storno"]==1)?'<img src="gui/check.gif" alt="" />':"&nbsp;";
			}
			$this->assign("status","Broj izdatih ugovora: ".count($res)."<br />Najveći broj ugovora: ".$max_ugo."<br />Ukupno predračuna: ".$pr_sum);
		}else{
			$this->assign("info_display"," display:none;");
			$this->assign("nores_start_comment","<!--");
			$this->assign("nores_end_comment","-->");
			$this->assign("nores_message","Nema izdatih ugovora za zadate kriterijume.");
		}
		$this->assign("issue_date",$dates);
		$this->assign("issue_ugo",$br_ugo);
		$this->assign("issue_pr",$kol_pr);
		$this->assign("issue_radnik",$radnik);
		$this->assign("issue_komentar",$kom);
		$this->assign("storno",$storno);
		
		echo $this->parseTemplate("ugovori/report_izd.tpl");
	}
	
	function reportUlaz($params){
		$start = $params["start"];
		$end = $params["end"];
		$start_ugo = $params["start_ugo"];
		$end_ugo = $params["end_ugo"];
		$bu = $params["br_ug"];
		$rad = $params["rad"];
		$kom = $params["kom"];
		$mat = ($params["mat"]);
		$stor = $params["stor"];
		
		$komentar = utf8RawUrlDecode($params["komentar"]);
		$komentar = utf8RawChrDecode($komentar);
		
		$prev= false;
		$whered = false;
		
		
		$q = "SELECT * FROM ugovori_ulaz";
		if($start || $end || $bu || $kom || $start_ugo || $end_ugo || $mat || $stor){
			$q .= " WHERE ";
			$whered = true;
		}
// FILTER
	// datum ulaza start
		if($start){
			$this->assign("start",$start);
			$filter_status .= "Datum od=".$start.", ";
			$tmp = explode(".",$start);
			$start = makeUnixTime(0,0,0,$tmp[1],$tmp[0],$tmp[2]);
		}
		// datum ulaza end
		if($end){
			$this->assign("end",$end);
			$filter_status .= "Datum do=".$end.", ";
			$tmp = explode(".",$end);
			$end = makeUnixTime(0,0,0,$tmp[1],$tmp[0],$tmp[2]);
		}
		$end = $end?$end+(60*60*24):$end;
		$prev = false;
		if($start || $end){
			if($start){
				$q .= "datum_ulaza >= ".$start;
				$prev = true;
			}
			if($end){
				if($prev)
					$q .= " AND";
				$q .= " datum_ulaza <= ".$end;
				$prev = true;
			}
		}else{
			$d=date("d");
			$m = date("m");
			$y = date("Y");
			$tt = makeUnixTime(0,0,0,$m,$d,$y);
			if(!$whered){
				$q .= " WHERE";
			}
			$q .= " datum_ulaza >= ".$tt;
			$prev = true;
			$this->assign("start",date("d.m.Y",$tt));
		}
	// datum ugovora start
		if($start_ugo){
			$this->assign("start_ugo",$start_ugo);
			$filter_status .= "Datum od=".$start_ugo.", ";
			$tmp = explode(".",$start_ugo);
			$start_ugo = makeUnixTime(0,0,0,$tmp[1],$tmp[0],$tmp[2]);
		}
	// datum ugovora end
		if($end_ugo){
			$this->assign("end_ugo",$end_ugo);
			$filter_status .= "Datum do=".$end_ugo.", ";
			$tmp = explode(".",$end_ugo);
			$end_ugo = makeUnixTime(0,0,0,$tmp[1],$tmp[0],$tmp[2]);
		}
		if($start_ugo || $end_ugo){
			if($start_ugo){
				if($prev){
					$q .= " AND";
				}
				$q .= " datum_ugovora >= ".$start_ugo;
				$prev = true;
			}
			if($end_ugo){
				if($prev)
					$q .= " AND";
				$q .= " datum_ugovora <= ".$end_ugo;
				$prev = true;
			}
		}
	// broj ugovora
		if($bu){
			$this->assign("filter_br_ugo",$bu);
			$filter_status .= "Broj ugovora=".$bu.", ";
			if($prev)
				$q .= " AND";
			$q .= " br_ugovora LIKE '%".$bu."%'";
			$prev = true;
		}
	// komitent
		if($kom){
			$this->assign("filter_komitent",$kom);
			$filter_status .= "Komitent=".$kom.", ";
			if($prev)
				$q .= " AND";
			$q .= " komitent LIKE '%".$kom."%'";
			$prev = true;
		}
	// radnik
		if(strlen(trim($rad))){
			$this->assign("filter_radnik",$rad);
			$filter_status .= "Radnik=".$rad.", ";
			$r = $this->_get("SELECT radnik_id FROM radnici WHERE CONCAT(radnik_ime,' ',radnik_prezime) LIKE '%".$rad."%'");
			if($r){
				if(!$prev)
					$q .= " WHERE";
				else
					$q .= " AND";
				$q.=" (";
				foreach($r as $k=>$v){
					$q .= " radnik=".$v["radnik_id"];
					if($k < (count($r)-1))
						$q .= " OR ";
				}
				$q .= ")";
			}else{
				if($prev)
					$q .= " AND radnik=-1";
				else
					$q .= " WHERE radnik=-1";
			}
		}
	// materijal
		if($mat > 0){
			if(!$prev){
				$q .= " WHERE";
			}else{
				$q .= " AND";
			}
			if($mat == 1){
				$q .= " materijal=0";
				$this->assign("sel_mat_0"," selected ");
			}else{
				$q .= " materijal=1";
				$this->assign("sel_mat_1"," selected ");
			}
		}
	// storno
		if($stor > 0){
			if(!$prev){
				$q .= " WHERE";
			}else{
				$q .= " AND";
			}
			if($stor == 1){
				$q .= " storno=0";
				$this->assign("sel_stor_0"," selected  ");
			}else{
				$q .= " storno=1";
				$this->assign("sel_stor_1"," selected ");
			}
		}
	// komentar
		if (strlen($komentar)>0) {
			$q .= " AND komentar LIKE '%$komentar%' ";
			$this->assign("filter_komentar",$komentar);
		}
		$q .= " ORDER BY datum_ulaza DESC";
		//echo $q;
		$res = $this->_get($q);
		if($res){
			$numRows = count($res);
			$today = date("d.m.y");
			$els = array();
			$els["dat_ulaz"] = $els["dat_ugo"] = $els["br_ugo"] = $els["kol_pr"] = $els["radnik"] = $els["komitent"] = array();
			$els["kampanja"] = $els["artikl"] = $els["iznos"] = $els["materijal"] = $els["komentar"] = array();
			foreach($res as $k=>$v){
				$dan = date("d.m.y",$res[$k]["datum_ulaza"]);
				$same_contract=false;
				if(($v["datum_ulaza"] == $res[$k-1]["datum_ulaza"]) &&
				   ($v["br_ugovora"] == $res[$k-1]["br_ugovora"]) &&
				   ($v["komitent"] == $res[$k-1]["komitent"])){
					$els["dat_ulaz"][] = "&nbsp;";
					$els["br_ugo"][] = "&nbsp;";
					$els["dat_ugo"][] = "&nbsp;";
					$els["kol_pr"][] = "&nbsp;";
					$els["radnik"][] = "&nbsp;";
					$els["komitent"][] = "&nbsp;";
					$numRows--;
					$same_contract=true;
					
				}else{
					$els["dat_ulaz"][] = date($this->getFromConfig("short_date"),$v["datum_ulaza"]);
					$els["br_ugo"][] = $v["br_ugovora"];
					$els["dat_ugo"][] = date($this->getFromConfig("short_date"),$v["datum_ugovora"]);
					$els["kol_pr"][] = $v["kol_predracuna"]." ";
					$q = "SELECT radnik_ime, radnik_prezime FROM radnici WHERE radnik_id=".$v["radnik"];
					$radnik = $this->db->select1($q);
					$radnik = $radnik[0];
					$radnik = @implode(" ",$radnik);
					$els["radnik"][] = $radnik;
					$els["komitent"][] = $v["komitent"];
				}
				$q2 = "SELECT kampanja_naziv FROM kampanje WHERE kampanja_id=".$v["kampanja"];
				$kampanjaName = $this->db->getOne($q2);
				$els["kampanja"][] = $kampanjaName;
				$els["id"][] = $res["id"];
				$tmp = explode(";",$v["artikl"],4);
				$artikli="";
				$art_id = $tmp[0];
				$art_tip = $tmp[1];
				$art_color = $tmp[2];
				$art_unit = $tmp[3];
				$art_name = $this->db->getOne("SELECT art_name FROM artikli WHERE art_id=".$art_id);
				$artikli = "<strong>$art_name</strong>";
				if($art_tip){
					$art_tip = $this->db->getOne("SELECT arttip_name FROM artikli_tipovi WHERE arttip_id=".$art_tip);
					$artikli.="<br />$art_tip";
				}
				if($art_color){
					$art_color = $this->db->getOne("SELECT art_boja_code FROM artikli_boje WHERE art_boja_id=".$art_color);
					$artikli.="<br />$art_color";
				}
				if($art_unit){
					$art_unit = $this->db->getOne("SELECT unit_svalue FROM jedinice_mere WHERE unit_id=".$art_unit);
					$artikli.="<br />$art_unit";
				}
				$els["artikl"][] = $artikli;
				
				$els["iznos"][] = $v["iznos"]." ";
				
				$mat = $v["materijal"];
				if($mat == 0){
					$els["materijal"][] = "&nbsp;";
				}else{
					$res_mat = $this->db->select2("SELECT datum, poreklo, potpun, napomena FROM ugovori_ulaz_materijal WHERE id=".$v["id"]." LIMIT 1");
					if(count($res_mat[0])){
						//$out = "<ul style=\"margin:0px; padding:0px; list-style-type:none;\">";
						$out = "";
						foreach ($res_mat as $row){
							/*$out .= "<li>".date($this->getFromConfig("short_date"),$row["datum"])."</li>";
							$out .= "<li>".$row["poreklo"]."</li>";
							if(!$row["potpun"])
								$out .= "<li>nepotpun</li>";
							if(strlen($row["napomena"]))
							$out .= "<li style=\"font-size:10px; font-style:italic;\">".nl2br($row["napomena"])."</li>";*/
							$out .= date($this->getFromConfig("short_date"),$row["datum"]).", ";
							$out .= $row["poreklo"];
							if(!$row["potpun"])
								$out .= ", nepotpun";
							if(strlen($row["napomena"]))
							$out .= ", ".nl2br($row["napomena"]);
						}
						$els["materijal"][] = $out;
					}else{
						$els["materijal"][] = "<img src=\"gui/check.gif\" />";
					}
				}
				
				$els["komentar"][] = nl2br($v["komentar"]." ");
				$els["storno"][] = $v["storno"]==0?"":"<img src=\"gui/check.gif\" />";
				
				
				//--------------------- VRACENJE UGOVORA
				if(!$same_contract){
					$vrati_btn = '<input type="button" class="vratiUgovorBtn" value="" title="vrati radniku" onclick="LoadRSDoc(\'ugovori.php?a=vracanje&mod=showVrati&id='.$v["id"].'\',\'ypAjaxForm\')" />';
					
					$q = "SELECT * FROM ugovori_ulaz_vracen WHERE ulaz_id=".$v["id"]." ORDER BY datum DESC";
					$res_vracen = $this->db->select2($q);
					
					if($res_vracen !== false){
						$last = $res_vracen[0];
						if ($last["status"] == 1) {
							$radnik = $last["radnik"];
							$comment = $last["komentar"];
							$id = $last["id"];
							$radnik = $this->db->getOne("SELECT CONCAT(radnik_ime,' ',radnik_prezime) FROM radnici WHERE radnik_id=$radnik");
							$els["vracen"][] = "<a title=\"Vrati ugovor u firmu...\" href=\"javascript:void(0);\" onclick=\"LoadRSDoc('ugovori.php?a=vracanje&mod=unesi&id=$id','afterVratiUgovor')\"><strong>$radnik</strong><br /><i>$comment</i></a>";
						}else{
							$els["vracen"][] = $vrati_btn;
						}
					}else{
						$els["vracen"][] = $vrati_btn;
					}
				}else{
					$els["vracen"][] = "";
				}
				
				if($dan == $today){
					$els["delete_ids"][] = '<a class="deleteItem" href="javascript:void(0);" onclick="if(confirm(\'Da li želite da obrišete ulaz:\n -broj ugovora: '.$res[$k]["br_ugovora"].'\n -artikl: '.$art_name.'\')){LoadRSDoc(\'ugovori.php?a=delitem&id='.$res[$k]["id"].'\',\'setContent\');}" title="obriši" id="deleteItem"><img src="gui/delUlazUgovor.gif" border="0" /></a>';
					//$els["delete_ids"][] = ' onmouseout="deleteUlazUgovora(0,this,event)" onmouseover="deleteUlazUgovora(\''.$res[$k]["id"]."-".$res[$k]["br_ugovora"].'\',this,event)"';
				}
			}
			$this->assign("delete_item",$els["delete_ids"]);
			$this->assign("id",$els["id"]);
			$this->assign("dat_ulaz",$els["dat_ulaz"]);
			$this->assign("br_ugo",$els["br_ugo"]);
			$this->assign("dat_ugo",$els["dat_ugo"]);
			$this->assign("kol_pr",$els["kol_pr"]);
			$this->assign("radnik",$els["radnik"]);
			$this->assign("komitent",$els["komitent"]);
			$this->assign("kampanja",$els["kampanja"]);
			$this->assign("artikl",$els["artikl"]);
			$this->assign("iznos",$els["iznos"]);
			$this->assign("materijal",$els["materijal"]);
			$this->assign("komentar",$els["komentar"]);
			$this->assign("storno",$els["storno"]);
			$this->assign("vracen",$els["vracen"]);

			$this->assign("affected_rows","Ukupno ugovora: ".$numRows);
		}else{
			$this->assign("info_display"," display:none;");
			$this->assign("nores_start_comment","<!--");
			$this->assign("nores_end_comment","-->");
			$this->assign("status","<p>Nema podataka o ulazu ugovora za zadate kriterijume.</p>");
		}
		echo $this->parseTemplate("ugovori/report_ulaz.tpl");
	}
	
	function reportUlazVracanje(){
		
		$filter_brugo = getFromInput("filter_br_ugo");
		$filter_radnik= getFromInput("filter_radnik");
		
		$this->assign("filter_br_ugo",$filter_brugo);
		$polja = $this->get_Radnici($filter_radnik);
			$this->assign("filter_radnik_id",$polja["polje_id"]);
			$this->assign("filter_radnik_ime",$polja["polje_ime"]);
			$this->assign("filter_radnik_prezime",$polja["polje_prezime"]);
			$this->assign("filter_radnik_selected",$polja["polje_selected"]);
			
			
		$q = "SELECT uuv.*, uu.br_ugovora, uu.datum_ulaza, rad.radnik_ime, rad.radnik_prezime "
			."FROM ugovori_ulaz_vracen uuv, ugovori_ulaz uu, radnici rad "
			."WHERE uuv.ulaz_id=uu.id "
			."AND rad.radnik_id=uuv.radnik";
		
		if($filter_brugo){
			$q .= " AND uu.br_ugovora LIKE '%$filter_brugo%'";
		}
		if($filter_radnik>0){
			$q .= " AND uuv.radnik=$filter_radnik";
		}
		
		$q .= " ORDER BY datum DESC, br_ugovora DESC, datum_ulaza DESC";
		$res = $this->db->select2($q);
		
		if($res !== false){
			$this->assign("info","Ukupno: ".count($res));
			$assign = array();
			foreach ($res as $i=>$row){
				$assign["row_id"][] = $i;
				
				$row["datum"] = date($this->getFromConfig("long_date"),$row["datum"]);
				$row["datum_ulaza"] = date($this->getFromConfig("long_date"),$row["datum_ulaza"]);
				$row["radnik"] = $row["radnik_ime"]." ".$row["radnik_prezime"];
				$row["komentar"] = nl2br($row["komentar"]);
				if($row["status"] == 1){
					$row["status"] = "gui/icons/user.gif";
					$row["status_title"] = "Kod radnika";
				}else{
					$row["status"] = "gui/yp_hand.gif";
					$row["status_title"] = "U firmi";
				}
				
				foreach ($row as $k=>$v){
					$assign[$k][] = $v;
				}
			}
			//print_rarray($assign);
			foreach ($assign as $k=>$v){
				$this->assign($k,$v);
			}
		}else{
			$this->assign("comment_start","<!--");
			$this->assign("comment_end","-->No results.");
		}
		
		echo $this->parseTemplate("ugovori/report_ulaz_vracanje.tpl");
	}
	
	function reportRadnik($params){
		$area_width = 600;
		
		$id = $params["id"];
		$ime = $params["first"];
		$prez = $params["last"];
		$od = $params["od"];
		$do = $params["do"];
		$tip = $params["tip"];
		
		$q_sum_izd	= "SELECT br_ugovora, datum FROM ugovori_izdavanje WHERE storno=0 AND radnik=$id";
		$q_sum_ul	= "SELECT DISTINCT br_ugovora FROM ugovori_ulaz WHERE radnik=$id";
		$q_sum_razd	= "SELECT br_ugovora, ur.datum FROM ugovori_razduzivanje ur, ugovori_izdavanje ui WHERE ui.id=ur.izdavanje_id AND ur.radnik=$id";
		
		if($od || $do){
			$this->assign("od",$od);
			$this->assign("do",$do);
			if($od){
				$tmp = explode(".",$od);
				$time_od = makeUnixTime(0,0,0,$tmp[1],$tmp[0],$tmp[2]);
				$q_sum_izd .= " AND datum >= $time_od";
				$q_sum_ul .= " AND datum_ulaza >= $time_od";
			}
			if($do){
				$tmp = explode(".",$do);
				$time_do = makeUnixTime(0,0,0,$tmp[1],$tmp[0],$tmp[2]);
				$q_sum_izd .= " AND datum <= $time_do";
				$q_sum_ul .= " AND datum_ulaza <= $time_do";
			}
			switch ($tip){
				case 0:		// izdati ugovori
					$q = "SELECT * FROM ugovori_izdavanje WHERE radnik=$id";
					if($od){
						$q .= " AND datum >= $time_od";
					}
					if($do){
						$q .= " AND datum <= $time_do";
					}
					$q .= " ORDER BY datum DESC";
					$res = $this->db->select2($q);
					
					if($res){
						foreach ($res as $row){
							$izd_id[] = $row["id"];
							$izd_date[] = date($this->_configArej["short_date"],$row["datum"]);
							$izd_brugo[] = $row["br_ugovora"];
							$izd_kolpr[] = ($row["kol_predracuna"]==0)?"0 ":$row["kol_predracuna"];
							$izd_stor[] = ($row["storno"]==0)?"&nbsp;":'<img src="gui/check.gif" />';
							$izd_kom[] = $row["komentar"];
						}
						$this->assign("id",$izd_id);
						$this->assign("datum",$izd_date);
						$this->assign("broj_ugovora",$izd_brugo);
						$this->assign("kol_predracuna",$izd_kolpr);
						$this->assign("storno",$izd_stor);
						$this->assign("komentar",$izd_kom);
						$out = $this->parseTemplate("ugovori/radnik_tbl_izd.tpl");
					}else{
						$out = "No records.";
					}
					
					$this->assign("selected_izd"," selected");
					break;
				case 1:		// usli ugovori
					$brojevi = array();
					$area_width = 800;
					
					$q = "SELECT * FROM ugovori_ulaz WHERE radnik=$id";
					if($od){
						$q .= " AND datum_ulaza >= $time_od";
					}
					if($do){
						$q .= " AND datum_ulaza <= $time_do";
					}
					$q .= " ORDER BY datum_ulaza DESC";
					$res = $this->db->select2($q);
					if($res){
						foreach ($res as $row){
							$tmp = explode(";",$row["artikl"]);
							$art_name = $this->db->getOne("SELECT art_name FROM artikli WHERE art_id=".$tmp[0]);
							$art_tip  = $this->db->getOne("SELECT arttip_name FROM artikli_tipovi WHERE arttip_id=".$tmp[1]);
							$art_boja = $this->db->getOne("SELECT art_boja_code FROM artikli_boje WHERE art_boja_id=".$tmp[2]);
							$art_unit = $this->db->getOne("SELECT unit_svalue FROM jedinice_mere WHERe unit_id=".$tmp[3]);
							
							$title = "";
							if($art_tip){
								$title .= $art_tip;
							}
							if($art_boja){
								if($art_tip)
									$title .= " - ";
								$title .= $art_boja;
							}
							if($art_unit){
								if($art_unit)
									$title .= " - ";
								$title .= " - $art_unit";
							}
							$artikl = "\r\n<ul class=\"artikli\"><li title=\"$title\">$art_name</li></ul>";
							if(in_array($row["br_ugovora"],$brojevi) === FALSE){
								$ul_id[]		= $row["id"];
								$ul_date_ul[]	= date($this->_configArej["short_date"],$row["datum_ulaza"]);
								$ul_brugo[]		= $row["br_ugovora"];
								$brojevi[]		= $row["br_ugovora"];
								$ul_date_ug[]	= date($this->_configArej["short_date"],$row["datum_ugovora"]);
								$ul_kolpr[]		= ($row["kol_predracuna"]==0)?"0 ":$row["kol_predracuna"];
								$ul_komitent[]	= $row["komitent"];
								$ul_kampanja[]	= $row["kampanja"];
								$ul_artikl[]	= $artikl;
								$ul_iznos[]		= ($row["iznos"]==0)?"0 ":$row["iznos"];
								$ul_materijal[] = ($row["materijal"]==0)?"&nbsp;":'<img src="gui/check.gif" />';
								$ul_stor[]		= ($row["storno"]==0)?"&nbsp;":'<img src="gui/check.gif" />';
								$ul_kom[]		= $row["komentar"];
							}else{
								$ul_artikl[count($ul_artikl)-1] .= $artikl;
								$ul_kom[count($ul_kom)-1] .= '<div style="border-top:1px solid #aaa;">'.$row["komentar"]."</div>";
							}
						}
						$this->assign("id",$ul_id);
						$this->assign("datum_ulaza",$ul_date_ul);
						$this->assign("broj_ugovora",$ul_brugo);
						$this->assign("datum_ugovora",$ul_date_ug);
						$this->assign("kol_pr",$ul_kolpr);
						$this->assign("komitent",$ul_komitent);
						$this->assign("kampanja",$ul_kampanja);
						$this->assign("artikl",$ul_artikl);
						$this->assign("iznos",$ul_iznos);
						$this->assign("materijal",$ul_materijal);
						$this->assign("storno",$ul_stor);
						$this->assign("komentar",$ul_kom);
						$out = $this->parseTemplate("ugovori/radnik_tbl_ul.tpl");
					}else{
						$out = "No records.";
					}
					
					$this->assign("selected_ul"," selected");
					break;
			}
			$this->assign("report1",$out);
		}
		$q_sum_izd .= " ORDER BY br_ugovora";
		$res_izd = $this->db->select2($q_sum_izd);
		$res_ulaz= $this->db->select2($q_sum_ul);
		$res_razd= $this->db->select2($q_sum_razd);
		
		if($res_izd){
			foreach ($res_izd as $k=>$v){
				$res_izd[$k] = $v["br_ugovora"];
				$ugoizd_dat[] = date($this->_configArej["short_date"],$v["datum"]);
			}
		}
		if($res_ulaz){
			foreach ($res_ulaz as $k=>$v){
				$res_ulaz[$k] = $v["br_ugovora"];
			}
		}
		if($res_izd && is_array($res_ulaz)){
			$ugovori_diff = array_diff($res_izd,$res_ulaz);
		}else{
			$ugovori_diff = $res_izd;
		}
		//var_dump($res_razd);
		if($res_razd){
			foreach ($res_razd as $k=>$v){
				$res_razd[$k] = $v["br_ugovora"];
			}
			$ugovori_diff = array_diff($ugovori_diff,$res_razd);
		}else{
			$res_razd = 0;
		}
		
		if(count($ugovori_diff)==1 && !$ugovori_diff[0]){
			$ugovori_diff = null;
		}
		
		$ugovori_diff = @array_values($ugovori_diff);
		$ugovori_kom = "'Ugovori koji su izdati komercijalisti a još nisu ušli u firmu:','";
		
		$this->assign("broj_ugovora_kom",$ugovori_diff);
		$this->assign("ugoizd_datum",$ugoizd_dat);
		
		$ugovori_kom .= $this->parseTemplate("ugovori/ugovori_kom.tpl")."'";
		$ugovori_kom = str_replace("\r\n","\\n",$ugovori_kom);
		$ugovori_kom = str_replace("\"","\\&quot;",$ugovori_kom);
		
		$izdato = count($res_izd)>0?count($res_izd):"0 ";
		if($izdato==1 && !is_array($res_izd)){
			$izdato = "0 ";
		}
		
		$uslo = count($res_ulaz)>0?count($res_ulaz):"0 ";
		if($uslo==1 && !is_array($res_ulaz)){
			$uslo = "0 ";
		}
		
		$razduzeno = is_array($res_razd)?count($res_razd):"0";
		
		$this->assign("radnik_id",$id);
		$this->assign("radnik_first",$ime);
		$this->assign("radnik_last",$prez);
		$this->assign("izdato",$izdato);
		$this->assign("uslo",$uslo);
		$this->assign("razduzeno",$razduzeno);
		$this->assign("kodkomercijaliste",count($ugovori_diff)." ");
		$this->assign("ugovori_diff",$ugovori_kom);
		$this->assign("search_compact",$this->parseTemplate("ugovori/search_compact.tpl"));
		$this->assign("area_width",$area_width);
		echo $this->parseTemplate("ugovori/report_radnik.tpl");
	}
	function reportRadnikSearch($name){
		$this->assign("search_kword",$name);
		$q = "SELECT DISTINCT radnik_id, radnik_ime, radnik_prezime "
			."FROM radnici, ugovori_izdavanje "
			."WHERE radnici.radnik_id=ugovori_izdavanje.radnik "
			."AND CONCAT(radnik_ime,' ',radnik_prezime) LIKE '%$name%'";
		$res = $this->_get($q);
		if(isset($res[0])){
			foreach ($res as $k=>$item){
				$radnik_id[] = $item["radnik_id"];
				$radnik_ime[] = $item["radnik_ime"];
				$radnik_prezime[] = $item["radnik_prezime"];
			}
			$this->assign("search_length",count($res));
			$this->assign("radnik_id",$radnik_id);
			$this->assign("radnik_first",$radnik_ime);
			$this->assign("radnik_last",$radnik_prezime);
		}else{
			$this->assign("search_length","0 ");
		}
		echo $this->parseTemplate("ugovori/search_list_radnik.tpl");
	}
	
	function reportMat($params,$page){
		foreach ($params as $k=>$v){
			$$k = $v;
			$this->assign($k,$v);
		}
		switch ($potpun){
			case 1:
				$this->assign("sel_potpun_0"," selected");
				break;
			case 2:
				$this->assign("sel_potpun_1"," selected");
				break;
		}
		switch ($tip){
			case 1:
				$this->assign("sel_tip_0"," selected");
				break;
			case 2:
				$this->assign("sel_tip_1"," selected");
				break;
		}
		
		$q = "SELECT uu.id, uu.br_ugovora, uu.datum_ulaza, uu.radnik, r.radnik_ime, r.radnik_prezime, uu.komitent, uu.artikl, "
			."um.datum, um.poreklo, um.potpun, um.napomena "
			."FROM ugovori_ulaz uu, ugovori_ulaz_materijal um, radnici r "
			."WHERE uu.id=um.id "
			."AND r.radnik_id=uu.radnik";
		
		if($start){
			$tmp = explode(".",$start);
			$t = makeUnixTime(0,0,0,$tmp[1],$tmp[0],$tmp[2]);
			$q .= " AND um.datum >= $t";
		}
		if($end){
			$tmp = explode(".",$end);
			$t = makeUnixTime(0,0,0,$tmp[1],$tmp[0],$tmp[2]);
			$q .= " AND um.datum <= $t";
		}
		if($start_ugo){
			$tmp = explode(".",$start_ugo);
			$t = makeUnixTime(0,0,0,$tmp[1],$tmp[0],$tmp[2]);
			$q .= " AND uu.datum_ulaza >= $t";
		}
		if($end_ugo){
			$tmp = explode(".",$end_ugo);
			$t = makeUnixTime(0,0,0,$tmp[1],$tmp[0],$tmp[2]);
			$q .= " AND uu.datum_ulaza <= $t";
		}
		if($br_ug){
			$q .= " AND uu.br_ugovora LIKE '%$br_ug%'";
		}
		if($rad){
			$q .= " AND CONCAT(r.radnik_ime,' ',r.radnik_prezime) like '%$rad%'";
		}
		if($kom){
			$q .= " AND uu.komitent like '%$kom%'";
		}
		if($potpun){
			$q .= " AND um.potpun=".($potpun-1);
		}
		if($tip){
			$q .= " AND um.poreklo like '%$tip%'";
		}
			
		$q .= " ORDER BY um.id ASC, um.datum DESC";
		
		$res = $this->db->select2($q);
		if(count($res[0])){
			
			$maxnum = 20;
			$num = count($res);
			if(!$page)$page=1;
			
			if($maxnum < $num){
				$q .= " LIMIT ".($page-1).",$maxnum";
				$res = $this->db->select2($q);
				$numpages = bcdiv($num,$maxnum,0);
				$numpages = ($num%$maxnum)>0?$numpages+1:$numpages;
				$this->assign("numpages",$numpages);
				
				//echo "Ukupno: $num<br />Max: $maxnum<br />Strana: $numpages";
				$pages = array();
				$pages_len = 20; // mora biti paran broj
				
				if($page<($pages_len+1)){
					$startpage = 1;
					$endpage = ($pages_len+1);
					if($numpages > $pages_len){
						$this->assign("after",'<li>...</li>');
					}
				}else{
					if($page>$numpages-$pages_len){
						$startpage = $numpages-$pages_len;
						$endpage = $numpages;
					}else{
						$startpage=$page-($pages_len/2);
						$endpage=$page+($pages_len/2);
						
						if($numpages > $pages_len){
							$this->assign("after",'<li>...</li>');
						}
						$this->assign("before",'<li>...</li>');
					}
				}
				
				for ($i=$startpage; $i<=$endpage; $i++){
					if($i>$numpages){
						break;
					}else{
						$pages[] = $i;
						if ($page == $i) {
							$curr[] = " id=\"current\"";
						}else{
							$curr[] = "";
						}
					}
				}
				$this->assign("page",$pages);
				$this->assign("current",$curr);
			}else{
				$this->assign("nodisplay"," style=\"display:none;\"");
			}
			//echo "<p>$q</p>";
			
			foreach ($res as $row){
				$ids[] = $row["id"];
				$br_ugo[] = $row["br_ugovora"];
				$dat_ugo[] = date($this->getFromConfig("short_date"),$row["datum_ulaza"]);
				$radnik[] = $row["radnik_ime"]." ".$row["radnik_prezime"];
				$komitent[] = $row["komitent"];
				
				$art = $row["artikl"];
				$artikl[] = implode(" - ",$this->getArticles($row["artikl"]));
				
				$datum_mat[] = date($this->getFromConfig("short_date"),$row["datum"]);
				$poreklo[] = $row["poreklo"];
				$potpuns[] = $row["potpun"]==1?"<img src=\"gui/check.gif\" align=\"absmiddle\" />":"&nbsp;";
				$napomena[] = $row["napomena"];
			}
			$this->assign("id",$ids);
			$this->assign("br_ugo",$br_ugo);
			$this->assign("dat_ugo",$dat_ugo);
			$this->assign("radnik",$radnik);
			$this->assign("komitent",$komitent);
			$this->assign("artikl",$artikl);
			
			$this->assign("datum_mat",$datum_mat);
			$this->assign("poreklo",$poreklo);
			$this->assign("potpun",$potpuns);
			$this->assign("napomena",$napomena);
		}else{
			$this->assign("start_comment","<!--");
			$this->assign("end_comment","-->Nema ulazaka materijala za zadate kriterijume.");
		}
		echo $this->parseTemplate("ugovori/report_mat.tpl");
	}
	
	function reportUlazIzlaz(){
		
		header("Content-Type:text/xls;");
		header("Content-Disposition: attachment; filename=ulaz_izlaz.xls");
		
		$q = "SELECT * FROM ugovori_ulaz";
		$usli = $this->db->select2($q);
		$usli_len = count($usli);
		
		$q = "SELECT * FROM ugovori_izdavanje";
		$izdati = $this->db->select2($q);
		$izdati_len = count($izdati);
		
		$q = "SELECT ui.br_ugovora, ui.id, ui.radnik "
			."FROM ugovori_razduzivanje ur, ugovori_izdavanje ui "
			."WHERE ur.izdavanje_id=ui.id";
		$razduzeni = $this->db->select2($q);
		
		//print_rarray($razduzeni);
		
		$razduzeni_len = count($razduzeni);
		
		$report = array();
		
		foreach ($usli as $ulaz){
			$report[$ulaz["radnik"]]["in"][] = $ulaz["br_ugovora"];
		}
		foreach ($izdati as $izdat){
			$report[$izdat["radnik"]]["out"][] = $izdat["br_ugovora"];
		}
		foreach ($razduzeni as $razd){
			$report[$razd["radnik"]]["back"][] = $razd["br_ugovora"];
		}
		$out = "Radnik\tizdati\tušli\trazduzeni\tostali\t";
		foreach ($report as $radnikid=>$data){
			
			$out .= "\r\n";
			
			if(is_array($data["in"])){
				$data["in"] = array_unique($data["in"]);
			}
			
			if(is_array($data["out"])){
				$data["out"] = array_unique($data["out"]);
			}
			
			if(is_array($data["back"])){
				$data["back"] = array_unique($data["back"]);
			}
			
			$q = "SELECT radnik_ime, radnik_prezime FROM radnici WHERE radnik_id=$radnikid";
			list($ime,$prezime) = $this->db->getRow($q);
			if(strlen($ime)){
				$out .="$ime $prezime";
			}else{
				$out .= "$radnikid";
			}
			$out .= "\t";
			
			if(is_array($data["out"])){
				$out .= count($data["out"])."\t";
			}else{
				$out .= "0\t";
			}
			
			if(is_array($data["in"])){
				$out .= count($data["in"])."\t";
			}else{
				$out .= "0\t";
			}
			
			if(is_array($data["back"])){
				$out .= count($data["back"])."\t";
			}else{
				$out .= "0\t";
			}
			
			if(is_array($data["in"])){
				if(is_array($data["out"])){
					$rest = array_diff($data["out"],$data["in"]);
				}else{
					$rest = array();
				}
			}else{
				if(is_array($data["out"])){
					$rest =$data["out"];
				}else{
					$rest = array();
				}
			}
			
			$out .= count($rest)."\t";
			
			if(count($rest)>1){
				$out .= implode(", ",$rest);
			}elseif (count($rest)==1){
				$out .= implode("",$rest);
			}
		}
		$out = nasaslova($out);
		echo $out;
	}
}
?>